//
//  HeadView.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "HeadView.h"

#import <UIImageView+WebCache.h>
#import "LRAFNetwork.h"
#import <AFNetworking.h>
#import "TopicModel.h"
#import "SmallCollectionViewCell.h"
#import "HeadCollectionViewCell.h"

@implementation HeadView
- (void)dealloc
{
    [_dataArr release];
    [super dealloc];
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatSubView];
        [self creatsmallview];
        [self post];
    }
    return self;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

//获取数据
-(void)post
{
    //
    NSString *str = @"http://api.meishi.cc/v5/index3.php?format=json?lon=4.9E-324&source=android&lat=4.9E-324&c_md5=dd16b0e80f4371ba35d39563ffdb101c&format=json&";
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *arr = responseObject[@"obj"][@"san_can"];
        self.dataArr = [ TopicModel modelHanderWithArray:arr];
        NSLog(@"~~~~~~~%@",_dataArr);
        [headView reloadData];
        [smallview reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}
//上边大的collectionVIew
-(void)creatSubView
{
    UICollectionViewFlowLayout  *layout = [[ UICollectionViewFlowLayout alloc] init];
    //布局设置
    //滚动方向
    layout.scrollDirection =  UICollectionViewScrollDirectionHorizontal;
    //cell大小
    layout.itemSize = CGSizeMake(375 * WIDTH  , HEIGHT*200);
    //行列间距
    //行
    layout.minimumLineSpacing = 0;
    //列
    layout.minimumInteritemSpacing = 0;
    //边界
    layout.sectionInset = UIEdgeInsetsMake(0,0,0,0);
    headView =[[ UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375 * WIDTH, 200*HEIGHT)   collectionViewLayout:layout];
    headView .delegate = self;
    headView.dataSource = self;
    headView.backgroundColor = [UIColor whiteColor];
    headView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self addSubview:headView];
    [headView setContentOffset:CGPointMake(375 * 15 *WIDTH, 0)];
    headView.pagingEnabled = YES;
    [headView registerClass:[HeadCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}

//中间小的collectionView
-(void)creatsmallview
{
    UICollectionViewFlowLayout  *layout = [[ UICollectionViewFlowLayout alloc] init];
    //布局设置
    //滚动方向
    layout.scrollDirection =  UICollectionViewScrollDirectionHorizontal;
    //cell大小
    layout.itemSize = CGSizeMake((WIDTH*375 - 40) /2 , HEIGHT*100);
    //行列间距
    //行
    layout.minimumLineSpacing = WIDTH*10;
    //列
    layout.minimumInteritemSpacing = 10 *HEIGHT;
    //边界
    layout.sectionInset = UIEdgeInsetsMake( WIDTH*10, HEIGHT*10, WIDTH*10, HEIGHT*10);
    smallview =[[ UICollectionView alloc] initWithFrame:CGRectMake(0, 200 * WIDTH, HEIGHT*375, 140 * WIDTH)   collectionViewLayout:layout];
    smallview .delegate = self;
    smallview.dataSource = self;
    smallview.backgroundColor = [UIColor whiteColor];
    smallview.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self addSubview:smallview];
    [smallview setContentOffset:CGPointMake(375 * 15 *WIDTH, 0)];
    smallview.pagingEnabled = YES;
    [smallview registerClass:[SmallCollectionViewCell class] forCellWithReuseIdentifier:@"smallcell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == headView) {
        //获取cell
       HeadCollectionViewCell *cell = [ collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        // cell.backgroundColor = [ UIColor greenColor];
        cell.title.text = [_dataArr[indexPath.item] title];
        cell.des.text = [_dataArr[indexPath.item] descr];
        [cell.img sd_setImageWithURL:[NSURL URLWithString:[_dataArr[indexPath.item] titlepic]]];
        cell.title.textColor = [ UIColor whiteColor];
        cell.des.textColor = [ UIColor whiteColor];
        return cell;
        
    }
    else if (collectionView == smallview)
    {
        SmallCollectionViewCell *cell = [ collectionView dequeueReusableCellWithReuseIdentifier:@"smallcell" forIndexPath:indexPath];
        [cell.img_s sd_setImageWithURL:[NSURL URLWithString:[_dataArr[indexPath.item] titlepic]]];
       // cell.blackView.hidden = NO;
        return cell;
    }
    else
    {
        return nil;
    }
}
//拖拽方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{ //如果点击的是大的视图
    if (scrollView == headView) {
        NSInteger currentItem = (scrollView.contentOffset.x) / 375 *WIDTH;
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:currentItem inSection:0];
        
       SmallCollectionViewCell *cell = [smallview   dequeueReusableCellWithReuseIdentifier:@"smallcell" forIndexPath:indexPath];
      
        //小的collectionView 也对应的滑到大的collectionView的item
        [smallview scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:currentItem inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        cell.blackView.hidden = YES;
      
        [smallview reloadData];
        
    }
    
}
//点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{     self.index = (long *)indexPath.item;
    //如果点击的为大的视图
    if (collectionView == headView) {
        TopicModel *t = _dataArr[indexPath.item];
        
         self.next(t.h_id);
    }
    //点击小的视图 大的视图变化
    if(collectionView == smallview)
    {
        //两个图都跟着改变
        [headView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        [smallview scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }

}

@end
